<?php
namespace app\index\controller;

class Goods extends Base
{
    public function index($id)
    {
	$goodsInfo = db('goods')->find($id);
	//商品主图信息数组
	$goodsThumbArr=array();
	if($goodsInfo['og_thumb']){
		$goodsThumbArr['sm_photo'] = $goodsInfo['sm_thumb'];
		$goodsThumbArr['mid_photo'] = $goodsInfo['mid_thumb'];
		$goodsThumbArr['big_photo'] = $goodsInfo['big_thumb'];
		$goodsThumbArr['og_photo'] = $goodsInfo['og_thumb'];
	}
	//获取当前商品相册信息
	$goodPhotoRes = db('goods_photo')->field('sm_photo,mid_photo,big_photo,og_photo')->where(array('goods_id'=>$id))->select();
	array_unshift($goodPhotoRes,$goodsThumbArr);  //将一个数组的元素放在该数组前面
	
	//获取图片并处理图片属性
	$gaArr = db('goods_attr')->alias('ga')->field('ga.*,a.attr_name,attr_type')->join('attr a',"ga.attr_id = a.id")->where(array('goods_id'=>$id))->select();
	$radioAttrArr=array();
	$uniAttrArr=array();
	foreach ($gaArr as $k => $v) {
		if($v['attr_type'] == 1){
			$radioAttrArr[$v['attr_id']][] = $v; 
		}else{
			$uniAttrArr[] = $v;
		}
	}
	// dump($radioAttrArr);die;
	$this->assign([
		'show_right'=>1,//文章列表和商品列表头部偏移判断
		'goodsInfo' => $goodsInfo,
		'goodPhotoRes' => $goodPhotoRes,
		'radioAttrArr' => $radioAttrArr,
		'uniAttrArr'  => $uniAttrArr,
	]);
	return view('goods');
   }
   public function ajaxGetMemberPrice($goods_id){
	   $price = model('goods')->getMemberPrice($goods_id);
	   return json($price);
   }
}
